#!/bin/bash
! [ -n "${MYSQL_PORT}" ] && exit 1
if [ -n "$(cat /etc/my.cnf | grep  port=)" ];
then
  #如果已存在该配置
  REPLACE_LINE=$(nl -b a /etc/my.cnf | grep port= | awk '{print $1}')
  eval "sed -i ${REPLACE_LINE}c\port=${MYSQL_PORT} /etc/my.cnf"
else
  #如果不存在该配置
  INSERT_LINE=$(nl -b a /etc/my.cnf | grep -w '\[mysqld\]' | awk '{print $1}')
  eval "sed -i ${INSERT_LINE}a\port=${MYSQL_PORT} /etc/my.cnf"
fi